package cn.iocoder.yudao.module.system.service.erp.customermanage;
import cn.iocoder.yudao.module.system.controller.admin.erp.customermanage.vo.CustomerManagePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.erp.customermanage.vo.CustomerManageSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.erp.customermanage.CustomerManageDO;
import cn.iocoder.yudao.module.system.dal.mysql.erp.customermanage.CustomerManageMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
import java.util.stream.Collectors;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.CUSTOMER_MANAGE_NOT_EXISTS;

/**
 * 客户管理员关系 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class CustomerManageServiceImpl implements CustomerManageService {

    @Resource
    private CustomerManageMapper customerManageMapper;

    @Override
    public Long createCustomerManage(CustomerManageSaveReqVO createReqVO) {
        // 插入
        CustomerManageDO customerManage = BeanUtils.toBean(createReqVO, CustomerManageDO.class);
        customerManageMapper.insert(customerManage);
        // 返回
        return customerManage.getId();
    }

    @Override
    public void updateCustomerManage(CustomerManageSaveReqVO updateReqVO) {
        // 校验存在
        validateCustomerManageExists(updateReqVO.getId());
        // 更新
        CustomerManageDO updateObj = BeanUtils.toBean(updateReqVO, CustomerManageDO.class);
        customerManageMapper.updateById(updateObj);
    }

    @Override
    public void deleteCustomerManage(Long id) {
        // 校验存在
        validateCustomerManageExists(id);
        // 删除
        customerManageMapper.deleteById(id);
    }

    private void validateCustomerManageExists(Long id) {
        if (customerManageMapper.selectById(id) == null) {
            throw exception(CUSTOMER_MANAGE_NOT_EXISTS);
        }
    }

    @Override
    public CustomerManageDO getCustomerManage(Long id) {
        return customerManageMapper.selectById(id);
    }

    @Override
    public PageResult<CustomerManageDO> getCustomerManagePage(CustomerManagePageReqVO pageReqVO) {
        return customerManageMapper.selectPage(pageReqVO);
    }

    @Override
    public List<Long> getCustomerIds(Long adminId) {
        return customerManageMapper.selectList(CustomerManageDO::getAdminId,adminId).stream().map(CustomerManageDO::getCustomerId).distinct().collect(Collectors.toList());
    }

    @Override
    public CustomerManageDO byCustomerId(Long customerId) {
        return customerManageMapper.selectOne(CustomerManageDO::getCustomerId,customerId);
    }

    @Override
    public void deleteAdminIdCustomerId(Long customerId,Long adminId) {
        customerManageMapper.delete(new LambdaQueryWrapper<CustomerManageDO>().eq(CustomerManageDO::getCustomerId,customerId).eq(CustomerManageDO::getAdminId,adminId));
    }

}